/**
 * File Name   : CharEncoderFilter.java
 * Description : 
 */
package com.hh.common.filter.character;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;

import com.hh.common.util.StringUtil;

/**
 * @author 冯昌硕
 * @create 2012-12-6 下午11:04:30
 */
public class CharEncoderFilter implements Filter {
    
    private static final Logger log = Logger.getLogger(CharEncoderFilter.class);

    private String encode;
    
    @Override
    public void destroy() {
        log.info("字符编码过滤器characterFilter销毁");
    }

    @Override
    public void doFilter(ServletRequest servReq, ServletResponse servRes,
            FilterChain chain) throws IOException, ServletException {
        servReq.setCharacterEncoding(encode);
        chain.doFilter(servReq, servRes);
    }

    @Override
    public void init(FilterConfig cfg) throws ServletException {
        String cfgEncode = cfg.getInitParameter("encode");
        if(StringUtil.isEmpty(cfgEncode)) {
            log.info("字符编码过滤器characterFilter的参数encode配置为空，取默认编码方式为UTF-8");
            encode = "UTF-8";
        } else {
            encode = cfgEncode;
            log.info("字符编码过滤器characterFilter的参数encode配置编码方式为:" + encode);
        }
    }

}
